<?php
namespace app\common\logic;

use app\common\component\user_wallet_silver\UserWalletSilverModel;
use app\common\component\user_wallet_silver\UserWalletSilverService;

class UserWalletSilver {
	/**
	 * 获取分红
	 */
	public function getInterest() {
		$user_id = \think\Session::get('user_id');
		$resultUserWalletSilvers = UserWalletSilverModel::where('status in(3, 4) AND user_id = ' . $user_id) -> order('user_wallet_silver_id ASC') -> select();
		if($resultUserWalletSilvers) {
			$price = 0;
			$WALLET_SILVER_INTEREST = getParam('WALLET_SILVER_INTEREST') / 100;

			foreach($resultUserWalletSilvers as $resultUserWalletSilver) {
				switch($resultUserWalletSilver -> getData('status')) {
					case 2:
					case 3:
						/*
						*/
						// 计算天数
						$days = countDays($resultUserWalletSilver -> start_time);
						$totalDays = countDays($resultUserWalletSilver -> start_time, $resultUserWalletSilver -> end_time);

						if($days > $totalDays) {
							$resultUserWalletSilver -> status = 4;

							$interest = $WALLET_SILVER_INTEREST * $resultUserWalletSilver -> price * $totalDays;

						} else {
							if($resultUserWalletSilver -> remaining_time > 0) {
								$days2 = countDays($resultUserWalletSilver -> remaining_time);
								$interest = $WALLET_SILVER_INTEREST * $resultUserWalletSilver -> price * $days2;
							} else {
								$interest = $WALLET_SILVER_INTEREST * $resultUserWalletSilver -> price * $days;
							}
						}

						$interest = round($interest, 2);
						if($interest != $resultUserWalletSilver -> interest) {
							$resultUserWalletSilver -> interest = $interest;
							$resultUserWalletSilver -> save();
						}

						$price += $resultUserWalletSilver -> price + $resultUserWalletSilver -> interest + $resultUserWalletSilver -> remaining_interest;
						break;
					case 4:
						$price += $resultUserWalletSilver -> price + $resultUserWalletSilver -> interest + $resultUserWalletSilver -> remaining_interest;
						break;
				}
			}

			return [
				'code' => 0,
				'data' => [
					'price' => $price
				]
			];
		} else {
			return [
				'code' => 0,
				'data' => [
					'price' => 0
				]
			];
		}
	}

	/**
	 * 提现
	 */
	public function withdraw($price) {
		$resultUserWalletSilvers = UserWalletSilverModel::where('status in(3, 4) AND user_id = ' . \think\Session::get('user_id')) -> order('start_time ASC') -> select();
		
		\think\Db::startTrans();
		try {
			foreach($resultUserWalletSilvers as $resultUserWalletSilver) {
				$currentPrice = $resultUserWalletSilver -> price + $resultUserWalletSilver -> interest + $resultUserWalletSilver -> remaining_interest;

				if($price >= $currentPrice) {
					$resultUserWalletSilver -> status = 2;
					$resultUserWalletSilver -> interest += $resultUserWalletSilver -> remaining_interest;
					$resultUserWalletSilver -> remaining_interest = 0;
					$resultUserWalletSilver -> remaining_time = 0;

					$price -= $currentPrice;
				} else {
					$data = [];
					$data['price'] = 0;
					if($price >= $resultUserWalletSilver -> price) {
						$data['price'] = $resultUserWalletSilver -> price;
						$resultUserWalletSilver -> price = 0;
						$price -= $data['price'];
					} else {
						$resultUserWalletSilver -> price -= $price;
						$data['price'] = $price;
						$price = 0;
					}

					if($price >= $resultUserWalletSilver -> remaining_interest) {
						$data['remaining_interest'] = $resultUserWalletSilver -> remaining_interest;
						$resultUserWalletSilver -> remaining_interest = 0;
						$price -= $data['remaining_interest'];
					} else {
						$resultUserWalletSilver -> remaining_interest -= $price;
						$data['remaining_interest'] = $price;
						$price = 0;
					}

					$resultUserWalletSilver -> interest -= $price;
					$data['interest'] = $price;

					$resultUserWalletSilver -> remaining_interest += $resultUserWalletSilver -> interest;
					$resultUserWalletSilver -> interest = 0;

					$resultUserWalletSilver -> remaining_time = time();

					$data['user_id'] = $resultUserWalletSilver -> user_id;
					$data['from_user_id'] = $resultUserWalletSilver -> from_user_id;
					$data['type'] = $resultUserWalletSilver -> getData('type');
					$data['status'] = 2;
					$data['interest'] += $data['remaining_interest'];
					$data['remaining_interest'] = 0;
					$data['start_time'] = $resultUserWalletSilver -> start_time;
					$data['end_time'] = $resultUserWalletSilver -> end_time;
					$data['remaining_time'] = 0;

					$createUserWalletSilver = (new UserWalletSilverService()) -> createV01($data);
					if($createUserWalletSilver['code'] != 0) {
						throw new \Exception($createUserWalletSilver['message']);
					}
				}

				if(!$resultUserWalletSilver -> save()) {
					throw new \Exception('银豆钱包提现失败');
				}

				if($price <= 0) {
					break;
				}
			}
			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '银豆钱包提现成功'
		];
	}
}
?>